package protocols

import (
	"fmt"
	"strconv"
)

type WebSocketBody struct {
	UUID    string                 `json:"uuid"`
	Action  int                    `json:"action"`
	Status  int                    `json:"status"`
	Message string                 `json:"message"`
	Params  map[string]interface{} `json:"params"`
	Data    interface{}            `json:"data"`
}

func (body *WebSocketBody) GetStringParam(key string, defValue ...string) (string, bool) {
	val, ok := body.Params[key]
	if ok {
		return fmt.Sprint(val), true
	}
	if len(defValue) == 0 {
		return "", false
	}
	return defValue[0], false
}

func (body *WebSocketBody) GetIntParam(key string, defValue ...int) (int, bool) {
	val, ok := body.Params[key]
	if ok {
		i, err := strconv.ParseInt(fmt.Sprint(val), 10, 32)
		if err != nil {
			return 0, false
		}
		return int(i), true
	}
	return 0, false
}
